<template>
  <a-row :gutter="10">
    <a-col :md="12" :sm="24">
      <a-card :bordered="false">

        <!-- 按钮操作区域 -->
        <a-row style="margin-left: 14px">
          <a-button @click="addBtn(2)" type="primary">添加子部门</a-button>
          <a-button @click="addBtn(1)" type="primary">添加一级部门</a-button>
          <a-button title="删除多条数据" type="default">批量删除</a-button>
          <!--<a-button @click="refresh" type="default" icon="reload" :loading="loading">刷新</a-button>-->
        </a-row>

        <div style="background: #fff;padding-left:16px;height: 100%; margin-top: 5px">
          <a-alert type="info" :showIcon="true">
            <div slot="message">
              当前选择：<span v-if="this.currSelected.title">{{ !this.currSelected.title ? '' : this.currSelected.title }}</span>
              <a v-if="this.currSelected.title" style="margin-left: 10px" @click="onClearSelected">取消选择</a>
            </div>
          </a-alert>
          <!--<a-input-search @search="onSearch" style="width:100%;margin-top: 10px" placeholder="请输入部门名称"/>-->
          <!--树-->
          <a-col :md="10" :sm="24">
            <template>
              <a-dropdown :trigger="[this.dropTrigger]" @visibleChange="dropStatus">
                <span style="user-select: none">
                  <tree
                    checkable
                    multiple
                    :selectedKeys="[currSelected.key]"
                    :treeData="departTree"
                    :checkStrictly="checkStrictly"
                    :checkedKeys="checkedKeys"
                    :expandedKeys="expandedKeys"
                    @check="onCheck"
                    @expand="onExpand"
                    @rightClick="rightHandle"
                    @select="onSelect"
                  />
                </span>
                <!--新增右键点击事件,和增加添加和删除功能-->
                <a-menu slot="overlay">
                  <a-menu-item @click="addBtn(3)" key="1">添加</a-menu-item>
                  <a-menu-item key="2">删除</a-menu-item>
                  <a-menu-item key="3">取消</a-menu-item>
                </a-menu>
              </a-dropdown>
            </template>
          </a-col>
        </div>
      </a-card>

      <div class="drawer-bootom-button">
        <a-dropdown :trigger="['click']" placement="topCenter">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="switchCheckStrictly(false)">父子关联</a-menu-item>
            <a-menu-item key="2" @click="switchCheckStrictly(true)">取消关联</a-menu-item>
            <a-menu-item key="3" @click="checkAll">全部勾选</a-menu-item>
            <a-menu-item key="4" @click="cancelCheckAll">取消全选</a-menu-item>
            <a-menu-item key="5" @click="expandAll">展开所有</a-menu-item>
            <a-menu-item key="6" @click="closeAll">合并所有</a-menu-item>
          </a-menu>
          <a-button>
            树操作 <a-icon type="up" />
          </a-button>
        </a-dropdown>
      </div>
    </a-col>

    <a-col :md="12" :sm="24">
      <a-tabs defaultActiveKey="1">
        <a-tab-pane tab="基本信息" key="1" >
          <depart-form :treeData="departTree" :formData="currSelected" ref="departForm" @ok="loadTree"></depart-form>
        </a-tab-pane>
        <a-tab-pane tab="部门权限" key="2" forceRender>
          <depart-permission-form :departId="currSelected.key"></depart-permission-form>
        </a-tab-pane>
      </a-tabs>
    </a-col>

    <depart-modal :treeData="departTree" ref="modal" @ok="loadTree"></depart-modal>
  </a-row>

</template>

<script>
import DepartForm from './modules/DepartForm'
import DepartModal from './modules/DepartModal'
import DepartPermissionForm from './modules/DepartPermissionForm'
import { Tree } from 'ant-design-vue'
import { apiQueryDepartTreeList, apiQueryDepartByKeywords } from '@/api/system'

export default {
  name: 'DepartListVue',
  components: {
    DepartForm, DepartModal, DepartPermissionForm, Tree
  },
  data () {
    return {
      departTree: [],
      dropTrigger: '',
      rightClickSelectedKey: '',
      currSelected: {},
      modalVisible: false,
      modalConfirmLoading: false,
      /** 树操作变量 start **/
      allTreeKeys: [],
      checkStrictly: true,
      checkedKeys: [],
      expandedKeys: []
      /** 树操作变量 end **/
    }
  },
  mounted () {
    this.loadTree()
  },
  methods: {
    loadTree () {
      this.departTree = []
      apiQueryDepartTreeList().then((res) => {
        if (res.success) {
          for (let i = 0; i < res.result.length; i++) {
            const temp = res.result[i]
            this.departTree.push(temp)
            this.getAllKeys(temp)
          }
        }
      })
    },
    getAllKeys (node) {
      this.allTreeKeys.push(node.key)
      if (node.children && node.children.length > 0) {
        for (let a = 0; a < node.children.length; a++) {
          this.getAllKeys(node.children[a])
        }
      }
    },
    // 右键点击下拉框改变事件
    dropStatus (visible) {
      if (visible === false) {
        this.dropTrigger = ''
      }
    },
    // 右键操作方法
    rightHandle (node) {
      this.dropTrigger = 'contextmenu'
      this.rightClickSelectedKey = node.node.eventKey
    },
    onSelect (selectedKeys, e) {
      const record = e.node.dataRef
      this.currSelected = Object.assign({}, record)
    },
    onClearSelected () {
      this.resetData()
    },
    resetData () {
      this.currSelected = {}
    },

    addBtn (type) {
      if (type === 1) {
        this.$refs.modal.show('新增', { orgCategory: '1' })
      } else if (type === 2) {
        const key = this.currSelected.key
        if (!key) {
          this.$message.warning('请先选中一条记录!')
          return false
        }
        this.$refs.modal.show('新增', { parentId: key, orgCategory: '2' })
      } else {
        this.$refs.modal.show('新增', { parentId: this.rightClickSelectedKey, orgCategory: '2' })
      }
    },

    modalCancel () {
      this.modalVisible = false
    },

    onSearch (value) {
      const that = this
      if (value) {
        apiQueryDepartByKeywords({ keyWord: value }).then((res) => {
          if (res.success) {
            that.departTree = []
            for (let i = 0; i < res.result.length; i++) {
              const temp = res.result[i]
              that.departTree.push(temp)
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      } else {
        that.loadTree()
      }
    },

    /** 树操作 **/
    // 勾选监听
    onCheck (o) {
      console.log(o)
      if (this.checkStrictly) {
        this.checkedKeys = o.checked
      } else {
        this.checkedKeys = o
      }
    },
    // 父子关联/取消
    switchCheckStrictly (v) {
      this.checkStrictly = v
    },
    // 全部勾选
    checkAll () {
      this.checkedKeys = this.allTreeKeys
    },
    // 取消全选
    cancelCheckAll () {
      this.checkedKeys = []
    },
    // 展开所有
    expandAll () {
      this.expandedKeys = this.allTreeKeys
    },
    // 展开监听
    onExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
    },
    // 合并所有
    closeAll () {
      this.expandedKeys = []
    }
  }
}
</script>

<style scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px
}

.drawer-bootom-button {
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: left;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
</style>
